package com.next

import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

@Transactional
@CompileStatic
@TypeChecked
class ProductService
{
    /*
    *初始化产品信息
    *author xiaruikun
    * */

    def initProduct()
    {
        if (!Product.find("from Product where name = '快贷'"))
        {
            def product = new Product(category: ProductCategory.find("from ProductCategory where name = '抵押贷'"), name: "快贷")
            product.save flush: true
        }

    }

    def initProductAccount1()
    {
        if (!ProductAccount.find("from ProductAccount where name = '快贷北京'"))
        {
            if (!Account.find("from Account where name = 'Default'"))
            {
                def c = new Account(name: "Default", type: AccountType.find("from AccountType where name = 'Partner'"), parent: Account.find("from Account where name = 'Default'"))
                c.save flush: true
            }

            //快贷北京
            def productAccount = new ProductAccount(name: "快贷北京", description: "快贷北京", maximumAmount: 3000000, minimumAmount: 100000, active: true, account: Account.find("from Account where name = 'Default'"), principalPaymentMethod: PrincipalPaymentMethod.find("from PrincipalPaymentMethod where name = '月息年本'"), product: Product.find("from Product where name = '快贷'"))
            productAccount.save flush: true

            def pmr = new ProductMortgageRate(mortgageRate: 0.7, assetType: AssetType.find("from AssetType where name =  '住宅'"), product: productAccount)
            pmr.save flush: true

            def pi = new ProductInterest(minimumRate: 0.001, maximumRate: 0.001, monthesOfStart: 24, monthesOfEnd: 36, contactLevel: ContactLevel.find("from ContactLevel where name = 'A'"), productInterestType: ProductInterestType.find("from ProductInterestType where name = '服务费费率'"), product: productAccount)
            pi.save flush: true

            pi = new ProductInterest(minimumRate: 0.001, maximumRate: 0.001, monthesOfStart: 24, monthesOfEnd: 36, contactLevel: ContactLevel.find("from ContactLevel where name = 'A'"), installments: true, productInterestType: ProductInterestType.find("from ProductInterestType where name = '服务费费率'"), product: productAccount)
            pi.save flush: true

            pi = new ProductInterest(minimumRate: 0.0088, maximumRate: 0.0088, monthesOfStart: 24, monthesOfEnd: 36, contactLevel: ContactLevel.find("from ContactLevel where name = 'A'"), installments: true, productInterestType: ProductInterestType.find("from ProductInterestType where name = '基本费率'"), product: productAccount)
            pi.save flush: true

            pi = new ProductInterest(minimumRate: 0.001, maximumRate: 0.001, monthesOfStart: 24, monthesOfEnd: 36, contactLevel: ContactLevel.find("from ContactLevel where name = 'B'"), productInterestType: ProductInterestType.find("from ProductInterestType where name = '信用调整'"), product: productAccount)
            pi.save flush: true

            pi = new ProductInterest(minimumRate: 0.002, maximumRate: 0.003, monthesOfStart: 24, monthesOfEnd: 36, contactLevel: ContactLevel.find("from ContactLevel where name = 'C'"), productInterestType: ProductInterestType.find("from ProductInterestType where name = '信用调整'"), product: productAccount)
            pi.save flush: true

            pi = new ProductInterest(minimumRate: 0, maximumRate: 0, monthesOfStart: 24, monthesOfEnd: 36, contactLevel: ContactLevel.find("from ContactLevel where name = 'A'"), productInterestType: ProductInterestType.find("from ProductInterestType where name = '渠道返费费率'"), product: productAccount)
            pi.save flush: true

            pi = new ProductInterest(minimumRate: 0, maximumRate: 0, monthesOfStart: 24, monthesOfEnd: 36, contactLevel: ContactLevel.find("from ContactLevel where name = 'A'"), productInterestType: ProductInterestType.find("from ProductInterestType where name = '二抵加收费率'"), product: productAccount)
            pi.save flush: true
        }
    }

    /*
    *校验产品信息
    *author xiaruikun
    *20170331
    * */

    def verifyInterest(OpportunityProduct opportunityProduct)
    {
        BigDecimal b1 = new BigDecimal(Double.toString(opportunityProduct.rate))
        BigDecimal b2 = new BigDecimal(Double.toString(100.00d))
        def interest = b1.divide(b2, 10, BigDecimal.ROUND_HALF_UP).doubleValue()
        List<ProductInterest> productInterest
        productInterest = ProductInterest.findAll("from ProductInterest where productInterestType.id=${opportunityProduct?.productInterestType?.id} and installments=${opportunityProduct.installments} and minimumRate<=${interest} and  monthesOfStart<=${opportunityProduct.opportunity.actualLoanDuration} and monthesOfEnd>=${opportunityProduct.opportunity.actualLoanDuration} and product.id=${opportunityProduct?.opportunity?.productAccount?.id}")
        if (productInterest && productInterest.size() > 0)
        {
            return true
        }
        else
        {
            return false
        }
    }
}
